# Dockerfile - CentOS 7 - RPM version
# https://github.com/openresty/docker-openresty

ARG RESTY_IMAGE_BASE="centos"
ARG RESTY_IMAGE_TAG="7"

FROM ${RESTY_IMAGE_BASE}:${RESTY_IMAGE_TAG}

LABEL maintainer="Evan Wies <evan@neomantra.net>"

ARG RESTY_IMAGE_BASE="centos"
ARG RESTY_IMAGE_TAG="7"
ARG RESTY_LUAROCKS_VERSION="3.7.0"
ARG RESTY_YUM_REPO="https://openresty.org/package/centos/openresty.repo"
ARG RESTY_RPM_FLAVOR=""
ARG RESTY_RPM_VERSION="1.19.3.1-1"
ARG RESTY_RPM_DIST="el7"
ARG RESTY_RPM_ARCH="x86_64"

LABEL resty_image_base="${RESTY_IMAGE_BASE}"
LABEL resty_image_tag="${RESTY_IMAGE_TAG}"
LABEL resty_luarocks_version="${RESTY_LUAROCKS_VERSION}"
LABEL resty_yum_repo="${RESTY_YUM_REPO}"
LABEL resty_rpm_flavor="${RESTY_RPM_FLAVOR}"
LABEL resty_rpm_version="${RESTY_RPM_VERSION}"
LABEL resty_rpm_dist="${RESTY_RPM_DIST}"
LABEL resty_rpm_arch="${RESTY_RPM_ARCH}"


# fix CVE 
# RUN yum update -y nss 
RUN yum update -y 

RUN yum install -y yum-utils \
    && yum-config-manager --add-repo ${RESTY_YUM_REPO} \
    && yum install -y \
        gettext \
        gzip \
        make \
        openresty${RESTY_RPM_FLAVOR}-${RESTY_RPM_VERSION}.${RESTY_RPM_DIST}.${RESTY_RPM_ARCH} \
        openresty-opm-${RESTY_RPM_VERSION}.${RESTY_RPM_DIST} \
        openresty-resty-${RESTY_RPM_VERSION}.${RESTY_RPM_DIST} \
        tar \
        unzip \
    && cd /tmp \
    && curl -fSL https://luarocks.github.io/luarocks/releases/luarocks-${RESTY_LUAROCKS_VERSION}.tar.gz -o luarocks-${RESTY_LUAROCKS_VERSION}.tar.gz \
    && tar xzf luarocks-${RESTY_LUAROCKS_VERSION}.tar.gz \
    && cd luarocks-${RESTY_LUAROCKS_VERSION} \
    && ./configure \
        --prefix=/usr/local/openresty/luajit \
        --with-lua=/usr/local/openresty/luajit \
        --lua-suffix=jit-2.1.0-beta3 \
        --with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1 \
    && make build \
    && make install \
    && cd /tmp \
    && rm -rf luarocks-${RESTY_LUAROCKS_VERSION} luarocks-${RESTY_LUAROCKS_VERSION}.tar.gz \
    && mkdir -p /var/run/openresty \
    && ln -sf /dev/stdout /usr/local/openresty/nginx/logs/access.log \
    && ln -sf /dev/stderr /usr/local/openresty/nginx/logs/error.log

# Add additional binaries into PATH for convenience
ENV PATH=$PATH:/usr/local/openresty/luajit/bin:/usr/local/openresty/nginx/sbin:/usr/local/openresty/bin

# Add LuaRocks paths
# If OpenResty changes, these may need updating:
#    /usr/local/openresty/bin/resty -e 'print(package.path)'
#    /usr/local/openresty/bin/resty -e 'print(package.cpath)'
ENV LUA_PATH="/usr/local/openresty/site/lualib/?.ljbc;/usr/local/openresty/site/lualib/?/init.ljbc;/usr/local/openresty/lualib/?.ljbc;/usr/local/openresty/lualib/?/init.ljbc;/usr/local/openresty/site/lualib/?.lua;/usr/local/openresty/site/lualib/?/init.lua;/usr/local/openresty/lualib/?.lua;/usr/local/openresty/lualib/?/init.lua;./?.lua;/usr/local/openresty/luajit/share/luajit-2.1.0-beta3/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;/usr/local/openresty/luajit/share/lua/5.1/?.lua;/usr/local/openresty/luajit/share/lua/5.1/?/init.lua"

ENV LUA_CPATH="/usr/local/openresty/site/lualib/?.so;/usr/local/openresty/lualib/?.so;./?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/openresty/luajit/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so;/usr/local/openresty/luajit/lib/lua/5.1/?.so"

#api addr
#ENV GameGateway_Rgs_Service="http://aa0574fc54ade47e3b8f9e06a1448c19-1967536988.ap-northeast-1.elb.amazonaws.com:20001/"
#ENV AccountGateWay_Service="http://a3634ec99255042cd92b8731918927ac-1194270993.ap-northeast-1.elb.amazonaws.com:20201/"

# compile lua-zlib
RUN yum install -y \
        git \
        cmake\
        gcc \
        zlib-devel \
    && cd /opt \
    && git clone https://github.com/brimworks/lua-zlib.git lua-zlib \
    && cd /opt/lua-zlib \
    && cmake -DLUA_INCLUDE_DIR=/usr/local/openresty/luajit/include/luajit-2.1 \
        -DLUA_LIBRARIES=/usr/local/openresty/luajit/lib \
        -DUSE_LUAJIT=ON \
        -DUSE_LUA=OFF \
    && make \
    && cp zlib.so /usr/local/openresty/lualib/zlib.so


# compile lua-bigdecimal
ARG LUA_BIGDECIMAL_VERSION="0.1.1"
RUN yum install -y \
        gcc-c++ \
    && cd /opt \
    && curl -fSL https://github.com/FastHCA/lua-bigdecimal/archive/refs/tags/v${LUA_BIGDECIMAL_VERSION}.tar.gz -o lua-bigdecimal-v${LUA_BIGDECIMAL_VERSION}.tar.gz \
    && tar xzf lua-bigdecimal-v${LUA_BIGDECIMAL_VERSION}.tar.gz \
    && cd /opt/lua-bigdecimal-${LUA_BIGDECIMAL_VERSION} \
    && make all \
        LUA_INCLUDE_DIR=/usr/local/openresty/luajit/include/luajit-2.1 \
        LUA_LIBRARIES=/usr/local/openresty/luajit/lib \
    && cp bigdecimal.so /usr/local/openresty/lualib/bigdecimal.so


RUN yum remove -y make \
    && yum clean all

# ADD openresty configuration files
ADD ./conf /app/conf
ADD ./script /app/script
ADD ./vhost /app/vhost

COPY docker-entrypoint.sh /docker-entrypoint.sh
ENTRYPOINT [ "sh", "/docker-entrypoint.sh" ]

# CMD ["/usr/bin/openresty", "-g", "daemon off;"]
CMD /usr/bin/openresty -p /app -g "daemon off;"

# Use SIGQUIT instead of default SIGTERM to cleanly drain requests
# See https://github.com/openresty/docker-openresty/blob/master/README.md#tips--pitfalls
STOPSIGNAL SIGQUIT
